/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package masterclient;

import Protocol.ReturnObject;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 *
 * @author Erik
 */
public class DialoogQueryCreate extends JDialog implements ActionListener{
    
    //Variabelen van DialoogQueryCreate
    private JPanel panel;
    private JLabel labelName = new JLabel("Query name");
    private JLabel labelQuery = new JLabel("Enter query");
    private JTextField fieldName = new JTextField(1);
    private JTextField fieldQuery = new JTextField(1);
    private JTextField fieldTest = new JTextField(1);
    private JButton buttonCreate = new JButton("Create");
    private JButton buttonTest = new JButton("Test the query");
    private Masterclient mc;
    private JComboBox jCombo;
    
    //Constructor
    public DialoogQueryCreate(Masterclient mc){
        this.mc = mc;
        //Standaard schermgegevens aanmaken
        setTitle("Create new query");
        setSize(320, 275);
        setBackground(Color.WHITE);
        setVisible(false);
        setResizable(false);
        
        //Toevoegen van de velden etc.
        createScreen();
        buttonCreate.addActionListener(this);
        buttonTest.addActionListener(this);
        buttonCreate.setEnabled(false);
    }
    
    //Methode om het scherm te maken
    public void createScreen(){
        //Eerst wordt een nieuw Jpanel aangemaakt, deze is zonder layout, alles wordt dus met coordinaten geplaatst
        panel = new JPanel();
        panel.setLayout(null);
        
        //Toevoegen labelName
        labelName.setBounds(10, 20, 70, 20);
        panel.add(labelName);
        
        //Toevoegen fieldName
        fieldName.setBounds(100, 20, 120, 20);
        panel.add(fieldName);
        
        //Toevoegen labelQuery
        labelQuery.setBounds(10, 50, 70, 20);
        panel.add(labelQuery);
        
        //Toevoegen fieldQuery
        fieldQuery.setBounds(10, 80, 300, 20);
        panel.add(fieldQuery);
        
        String[] s = {"Active", "DeActive"};
        jCombo = new JComboBox(s);        
        jCombo.setBounds(10, 110, 70, 20);
        add(jCombo);
        
        //Toevogen create button
        buttonCreate.setBounds(10, 140, 75, 20);
        panel.add(buttonCreate);
        
        //Toevogen test button
        buttonTest.setBounds(10, 170, 120, 20);
        panel.add(buttonTest);
        
        //Toevoegen test field
        fieldTest.setBounds(10, 200, 120, 20);
        fieldTest.setEnabled(false);
        fieldTest.setText("");
        panel.add(fieldTest);
        
        //Zorgt ervoor dat het panel toegevoegd wordt aan het scherm
        add(panel);
    }
    
    /**
    * Methode om de juiste int terug te geven voor isActive (1 = activet)
    * @author Michael Stevens
    * @return int met isACtive
    * @since 30-05-2011
    */
    public int isActiveQ(){
        int m;
        String s = jCombo.getSelectedItem().toString();
        if(s.equals("Active")){
            m = 1;
        }else{
            m = 0;
        }
        return m;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
         if(e.getSource().equals(buttonCreate)){
             if(fieldQuery.getText().equals("") || fieldName.getText().equals("")){
                mc.getGUI().showMessageD("Please fill out all fields");
            }else{
                mc.getQm().createQuery(fieldQuery.getText(), fieldName.getText(), isActiveQ());
                setVisible(false);
            }
         }else if(e.getSource().equals(buttonTest)){
             ReturnObject ro = mc.getAuth().excecuteTestSelect(fieldQuery.getText());
             if(ro != null){
                if(ro.getMessage().equals("ok")){
                    fieldTest.setText("succesvol");
                    buttonCreate.setEnabled(true);
                }else{
                    fieldTest.setText("fout");
                    buttonCreate.setEnabled(false);
                }
             }else{
                 fieldTest.setText("fout");
                 buttonCreate.setEnabled(false);
             }
         }
    }
    
}
